package com.company.cloud.user.client;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.user.modules.api.request.RegisterNumDataRequest;
import com.company.cloud.user.modules.api.response.RegisterNumDataResponse;
import com.company.cloud.user.modules.api.service.UserDataRemoteService;
import com.company.cloud.user.modules.base.entity.RoleInfo;
import com.company.cloud.user.modules.base.service.RoleInfoService;
import com.company.cloud.user.modules.cache.models.CustomerCountModel;
import com.company.cloud.user.modules.mgr.service.UserCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@Service
public class UserDataRemoteServiceImpl implements UserDataRemoteService {
    @Autowired
    private UserCountService userCountService;
    @Autowired
    private RoleInfoService roleInfoService;

    @Override
    public BaseClientResult<RegisterNumDataResponse> registerUserNum(RegisterNumDataRequest params) {
        try {
            Integer num= userCountService.userNum(params);
            RegisterNumDataResponse data = new RegisterNumDataResponse();
            data.setNum(num==null?0:num);
            return BaseClientResult.success(data);
        }catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<Integer,Integer>> customerCount(Integer userId) {
        try {
            Map<Integer,Integer> data = new HashMap<>();
            CustomerCountModel model = userCountService.getCustomerCount(userId);
            if (null != model) {
                data.put(model.getUserId(),model.getCustomerCount());
            }
            return BaseClientResult.success(data);
        }catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<Map<Integer,Integer>> customerCountMap(Set<Integer> userId) {
        try {
            Map<Integer,Integer> data = new HashMap<>();
            List<CustomerCountModel> list = userCountService.getCustomerCount(userId);
            if (!CollectionUtils.isEmpty(list)) {
                data = list.stream().collect(Collectors.toMap(item-> item.getUserId(),
                        item -> item.getCustomerCount()));
            }
            return BaseClientResult.success(data);
        }catch (Exception e){
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> wjjTest(String code) {
        System.out.println("+++++++++++++wjjTest++++++++++++++++++++");
//        System.out.println("GTS Transaction Begins wjjTest: " + TxcContext.getCurrentXid());
//        LOGGER.info("Account Service ... xid: " + RootContext.getXID());
        try {
//            RoleInfo entity=new RoleInfo();
//            entity.setCode(code);
//            entity.setCompanyNo("wjj");
//            entity.setName("wjj"+ IdUtil.fastSimpleUUID());
//            entity.setIsSys(BaseConstant.YNEnum.NO.getCode());
//            entity.setCdt(LocalDateTime.now());
//            entity.setEdt(LocalDateTime.now());
//            entity.setCip("127");
//            entity.setEip("127");
//            entity.setCman(1);
//            entity.setEman(1);
//            roleInfoService.save(entity);
            return  BaseClientResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return BaseClientResult.error();
        }
    }


}
